#ifndef STACKTAB_H
#define STACKTAB_H

#include <QObject>
#include "../../XXqtDefine.h"
#include <QStackedWidget>
#include "../QAbstractButton/ButtonGroup.h"
#include "../QStackedWidget/StackedWidgetShell.h"

class StackTab : public QObject
{
    Q_OBJECT
public:
    explicit StackTab(QObject *parent = nullptr);

    void shell(QStackedWidget *stack, QWidget *bar);
    StackedWidgetShell& stackedShell(){return _stackedShell;}
    ButtonGroup& buttonGroupShell(){return _buttonGroup;}

    // config

    // action
    template<class Widget>
    Widget* add(const QString &name, const QString &text=""){
        Widget *widget = new Widget(_stackedWidget);
        widget->setObjectName(name);
        bool ret = add<QPushButton>(name,text,widget);
        if(!ret){
            delete widget;
            return nullptr;
        }
        return widget;
    }
    template<class Widget, class Button>
    Widget* add(const QString &name, const QString &text){
        Widget *widget = new Widget(_stackedWidget);
        widget->setObjectName(name);
        bool ret = add<Button>(name,text,widget);
        if(!ret){
            delete widget;
            return nullptr;
        }
        return widget;
    }
    template<class Button>
    bool add(const QString &name, const QString &text, QWidget *widget){
        /**
          note:
          bar maybe have been buttons when shell, and need to add to match the corresponding button here,
          so could not return when failure to add button to ButtonGroup.
          */
        if(_stackedShell.contains(name)){
            return false;
        }
        _buttonGroup.add<Button>(name,text);
        _stackedShell.add(name,widget);
        return true;
    }
    void select(const QString &name);
    QString currentSelected();

    // signal
signals:
    void sigCurrentTabChanged(QString current, QString previous);

private slots:
    void buttonGroup_onSelectionChanged(QStringList current, QStringList previous);

private:
    QStackedWidget *_stackedWidget;
    StackedWidgetShell _stackedShell;
    ButtonGroup _buttonGroup;
};

#endif // STACKTAB_H
